package com.ymatou.iapi.liveseller.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.liveseller.service.CreateLiveActivityCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ActivityCallService;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveActivity;
import com.ymttest.database.model.LiveActivityProduct;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

/**
 * 创建直播活动 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_CreateLiveActivity {
	private static CreateLiveActivityBean createliveactivityBean;
	private static CreateLiveActivityCall createliveactivityCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建直播活动");
	}

	@Before
	public void caseUp() {
		createliveactivityBean = new CreateLiveActivityBean();
		createliveactivityCall = new CreateLiveActivityCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createliveactivityCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(CreateLiveActivityBean createliveactivityBean2) throws InterruptedException {
		LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
		Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean2.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
		LiveActivity activity = LiveRoomWapper.selectLiveactivityById(liveacctivityid);
		Logger.verifyEquals(0, activity.getActivityState(), "验证ActivityState");
		// 通知由job决定 发送过了为1
		Logger.verifyNotNull(activity.getBuyerNotify(), "验证BuyerNotify");
		Logger.verifyNotNull(activity.getSellerNotify(), "验证SellerNotify");

		Logger.verifyNotNull(activity.getCreateTime(), "验证CreateTime");
		Logger.verifyEquals(createliveactivityBean2.getLiveActivity().getSellerId(), activity.getSellerId(), "验证SellerId");
		// 可能有误差
		Logger.verifyEquals(Long.valueOf(createliveactivityBean2.getLiveActivity().getStartTime()) / 10000, activity.getStartTime().getTime() / 10000, "验证StartTime");
		Logger.verifyEquals(Long.valueOf(createliveactivityBean2.getLiveActivity().getEndTime()) / 10000, activity.getEndTime().getTime() / 10000, "验证EndTime");
		Integer stock = createliveactivityBean2.getLiveActivity().getActivityProducts().stream().collect(Collectors.summingInt(ActivityProductDto::getActivityStock));
		createliveactivityBean2.getLiveActivity().getActivityProducts().forEach(ap -> {
			try {
				LiveActivityProduct aproduct = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, ap.getProductId()).get(0);

				Logger.verifyEquals(createliveactivityBean2.getLiveActivity().getLiveId(), aproduct.getLiveId(), "验证liveid");
				Logger.verifyEquals(createliveactivityBean2.getLiveActivity().getSellerId(), aproduct.getSellerId(), "验证SellerId");
				Logger.verifyEquals(stock, aproduct.getActivityStock(), "验证ActivityStock");
				//规格库存和价格要在库存服务维护
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateLiveActivity_001() {
		Logger.start(true, "创建直播活动,成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("200", createliveactivityCall.getString("Code"), "验证成功");
			verifyDB(createliveactivityBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_002() {
		Logger.start(true, "不传参数LiveActivity失败");
		try {
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("400", createliveactivityCall.getString("Code"), "验证错误");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateLiveActivity_003() {
		Logger.start(true, "创建直播活动,成功,多规格");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			for (int i = 0; i < 2; i++) {
				ActivityProductDto apd = new ActivityProductDto();
				apd.setActivityPrice(5.0d);
				apd.setActivityStock(10);
				apd.setCatalogId(cids.get(i));
				apd.setProductId(productId);
				ActivityProducts.add(apd);
			}
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("200", createliveactivityCall.getString("Code"), "验证成功");
			verifyDB(createliveactivityBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
//	#直播活动最多添加商品数 配置决定
//	liveActivityProductCount=10
	public void Tc_CreateLiveActivity_004() {
		Logger.start(true, "创建直播活动,多商品失败");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			List<String> cids2 = CIWapper.selectCatalogsByProductId(productId2).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			// Logger.comment("添加第一个商品");
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			// Logger.comment("添加第二个商品");
			ActivityProductDto apd2 = new ActivityProductDto();
			apd2.setActivityPrice(5.0d);
			apd2.setActivityStock(10);
			apd2.setCatalogId(cids.get(0));
			apd2.setProductId(productId2);
			ActivityProducts.add(apd2);

			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证失败");
			// 一场秒杀最多只支持1个商品
			Logger.verifyIsContains("一场秒杀最多只支持1个商品", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_005() {
		Logger.start(false, "直播已结束创建秒杀活动失败");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			LiveRoomCallService.AutoCloseLiveRoomByUserId(sellerId);
			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()-1 * 60 * 1000));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 10 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("直播已关闭", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_006() {
		Logger.start(false, "秒杀时间不可早于直播开始时间");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			
			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()-1 * 60 * 1000));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 10 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("秒杀时间不可早于直播开始时间", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//此场景现在取消判断 20181220 @陈飞
	public void Tc_CreateLiveActivity_007() {
		Logger.start(false, "秒杀时间不可晚于直播结束时间");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			
			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(endtime.getTime()+1 * 60 * 1000));
			LiveActivity.setEndTime(String.valueOf(endtime.getTime() + 10 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("秒杀时间不可晚于直播结束时间", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_008() {
		Logger.start(false, "秒杀活动必须设置至少一个商品");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
//			ActivityProductDto apd = new ActivityProductDto();
//			apd.setActivityPrice(5.0d);
//			apd.setActivityStock(10);
//			apd.setCatalogId(cids.get(0));
//			apd.setProductId(productId);
//			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("400", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("不能为空", createliveactivityCall.getString("Msg"), "验证Msg");
			//秒杀活动必须设置至少一个商品
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_009() {
		Logger.start(false, "开始时间与其他秒杀冲突");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("开始时间与其他秒杀冲突", createliveactivityCall.getString("Msg"), "验证Msg");
			//开始时间与其他秒杀冲突
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_010() {
		Logger.start(false, "规格不存在");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0)+1);
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("规格不存在", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_011() {
		Logger.start(false, "秒杀价不得高于当前售价");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(115.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("秒杀价不得高于当前售价", createliveactivityCall.getString("Msg"), "验证Msg");
			//秒杀价不得高于当前售价
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_012() {
		Logger.start(false, "商品规格库存已售罄");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			CIWapper.updatestocknumByProductID(0, productId);
			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("商品规格库存已售罄不可添加至秒杀中", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_CreateLiveActivity_013() {
		Logger.start(false, "活动库存超出最大库存");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			CIWapper.updatestocknumByProductID(10, productId);
			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(20);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			Logger.verifyIsContains("活动库存超出最大库存", createliveactivityCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P2.class })
	@TestCase
	//不能和砍价团冲突 @陈飞 活动站点改造 TODO 待确认高低优先级？
//	@Ignore
	public void Tc_CreateLiveActivity_014() {
		Logger.start(false, "有其他交易隔离活动冲突失败有其他交易隔离活动冲突失败");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			long ys=10000;
			Date starttime = new Date(now.getTime() + ys);
			Date endtime = new Date(now.getTime() + 70 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建交易隔离活动");
//			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			Integer activityId = ActivityCallService.createActivityInProcess(MapUtil.hashMap("ActivityTemplateId",2));
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,cids,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			Thread.sleep(1000);
			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(YMTDateUtil.getDate().getTime()+ys));
			//这里的时间范围差了2小时 不确定是哪里的时间不一致
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 120 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("201", createliveactivityCall.getString("Code"), "验证成功");
			//CheckProductsInTradeIsolation
			Logger.verifyIsContains("该活动商品无法加入秒杀", createliveactivityCall.getString("Msg"), "验证Msg");
			Logger.debug(YMTDateUtil.getFormatTime(new Date(Long.valueOf(LiveActivity.getStartTime())), YMTDateUtil.YMDHMS));
			Logger.debug(YMTDateUtil.getFormatTime(new Date(Long.valueOf(LiveActivity.getEndTime())), YMTDateUtil.YMDHMS));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateLiveActivity_015() {
		Logger.start(true, "创建直播活动,成功/计数器+1");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			Date starttime = new Date(now.getTime() + 5 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);

			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());

			Logger.comment("创建秒杀活动");
			LiveActivityDto LiveActivity = new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts = new ArrayList<>();
			ActivityProductDto apd = new ActivityProductDto();
			apd.setActivityPrice(5.0d);
			apd.setActivityStock(10);
			apd.setCatalogId(cids.get(0));
			apd.setProductId(productId);
			ActivityProducts.add(apd);
			LiveActivity.setActivityProducts(ActivityProducts);
			// Logger.debug(createlive.getStartTime());
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime() + 1 * 60 * 1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			Integer counter = CounterCallService.getSingleValueByLiveId(createlive.getId());
//			CounterCallService.getSingleValue("","","");
			createliveactivityBean.setLiveActivity(LiveActivity);
			createliveactivityCall.setData(createliveactivityBean);
			createliveactivityCall.callService();
			Logger.verifyEquals("200", createliveactivityCall.getString("Code"), "验证成功");
			Integer counter1 = CounterCallService.getSingleValueByLiveId(createlive.getId());
			Logger.verifyEquals(counter+2, counter1, "添加商品之后，计数器+1");
			
			verifyDB(createliveactivityBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
}